package com.sora.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

/**
 * @Classname SoraBeanUtils
 * @Description
 * @Date 2024/03/21 10:27
 * @Author by Sora33
 */
@Component
public class SoraBeanUtils implements BeanFactoryPostProcessor {

    private static ConfigurableListableBeanFactory configurableListableBeanFactory;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        SoraBeanUtils.configurableListableBeanFactory = beanFactory;
    }

    public static <T> T getBean(String name) {
        return (T) configurableListableBeanFactory.getBean(name);
    }

    public static <T> T getBean(Class<T> cls) {
        return (T) configurableListableBeanFactory.getBean(cls);
    }
}
